const express = require('express');
const router = express.Router();
const { authenticateToken, checkRole } = require('../common/auth');
const petController = require('../controller/petController');

// 添加调试日志中间件
router.use((req, res, next) => {
  console.log(`[Pets Router] ${req.method} ${req.path}`);
  next();
});

/**
 * @swagger
 * /api/pets/categories:
 *   post:
 *     tags: [Pets]
 *     summary: 创建宠物分类
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *     responses:
 *       201:
 *         description: 分类创建成功
 */
router.post('/categories', authenticateToken, checkRole(['admin']), petController.createCategory);

/**
 * @swagger
 * /api/pets/categories:
 *   get:
 *     tags: [Pets]
 *     summary: 获取所有宠物分类
 *     responses:
 *       200:
 *         description: 成功获取分类列表
 */
router.get('/categories', petController.getAllCategories);

/**
 * @swagger
 * /api/pets:
 *   post:
 *     tags: [Pets]
 *     summary: 创建宠物信息
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *             properties:
 *               name:
 *                 type: string
 *               type:
 *                 type: string
 *               age:
 *                 type: integer
 *               price:
 *                 type: number
 *               description:
 *                 type: string
 *     responses:
 *       201:
 *         description: 宠物创建成功
 */
router.post('/', (req, res, next) => {
  console.log('[Pets Router] POST / - 进入创建宠物路由');
  petController.createPet(req, res, next);
});

/**
 * @swagger
 * /api/pets:
 *   get:
 *     tags: [Pets]
 *     summary: 获取宠物列表
 *     parameters:
 *       - in: query
 *         name: category_id
 *         schema:
 *           type: integer
 *         description: 按分类筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 按状态筛选
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取宠物列表
 */
router.get('/', (req, res, next) => {
  console.log('[Pets Router] GET / - 进入获取宠物列表路由');
  petController.getAllPets(req, res, next);
});

/**
 * @swagger
 * /api/pets/{id}:
 *   get:
 *     tags: [Pets]
 *     summary: 获取宠物详情
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 宠物ID
 *     responses:
 *       200:
 *         description: 成功获取宠物详情
 */
router.get('/:id', petController.getPetById);

/**
 * @swagger
 * /api/pets/{id}:
 *   put:
 *     tags: [Pets]
 *     summary: 更新宠物信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 宠物ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               category_id:
 *                 type: integer
 *               breed:
 *                 type: string
 *               age:
 *                 type: integer
 *               price:
 *                 type: number
 *               description:
 *                 type: string
 *               image_url:
 *                 type: string
 *               status:
 *                 type: string
 *     responses:
 *       200:
 *         description: 宠物信息更新成功
 */
router.put('/:id', authenticateToken, petController.updatePet);

/**
 * @swagger
 * /api/pets/{id}:
 *   delete:
 *     tags: [Pets]
 *     summary: 删除宠物
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 宠物ID
 *     responses:
 *       200:
 *         description: 宠物删除成功
 */
router.delete('/:id', authenticateToken, petController.deletePet);

module.exports = router; 